﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
using System.Data.OleDb;
using System.Net;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;


namespace QuanLyNhanVien.DataAccessLayer
{
    class NhanVienDAO : DataProvider
    {

        private string URLNhanVien = URL + "/api/NhanVien";
        public List<string> GetColName()
        {
            //List<string> cols = new List<string>();
            //string query = "SELECT * FROM NHANVIEN";
            //DataTable dt = new DataTable();
            //dt = executeQuery(query);
            //for (int i = 0; i < dt.Columns.Count; i++)
            //{
            //    cols.Add(dt.Columns[i].ToString());
            //}
            //return cols;

            List<string> result = new List<string>();

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL + "/api/Tables/NHANVIEN");
            request.Method = "GET";
            request.ContentType = "application/json";

            try
            {
                WebResponse webResponse = request.GetResponse();
                using (Stream webStream = webResponse.GetResponseStream())
                {
                    if (webStream != null)
                    {
                        using (StreamReader responseReader = new StreamReader(webStream))
                        {

                            string response = responseReader.ReadToEnd();
                            result = JsonConvert.DeserializeObject<List<string>>(response);

                        }
                    }
                }
            }
            catch (Exception e)
            {
            }

            return result;
        }
        public List<NhanVienDTO> LayDanhSachNhanVien()
        {     
            List<NhanVienDTO> result = new List<NhanVienDTO>();

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URLNhanVien);
            request.Method = "GET";
            request.ContentType = "application/json";
            
            try
            {
                WebResponse webResponse = request.GetResponse();
                using (Stream webStream = webResponse.GetResponseStream())
                {
                    if (webStream != null)
                    {
                        using (StreamReader responseReader = new StreamReader(webStream))
                        {

                            string response = responseReader.ReadToEnd();
                            result = JsonConvert.DeserializeObject<List<NhanVienDTO>>(response);

                        }
                    }
                }
            }
            catch (Exception e)
            {
            }
            
            return result;
        }

        public bool ThemNhanVien(NhanVienDTO nv)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URLNhanVien);

            request.Method = "POST";
            request.ContentType = "application/json";



            JObject jObject = JObject.FromObject(nv);
            string data = jObject.ToString(Newtonsoft.Json.Formatting.None);


            request.ContentLength = data.Length;




            using (Stream webStream = request.GetRequestStream())
            using (StreamWriter requestWriter = new StreamWriter(webStream, System.Text.Encoding.ASCII))
            {
                requestWriter.Write(data);
            }

            try
            {
                WebResponse webResponse = request.GetResponse();
                using (Stream webStream = webResponse.GetResponseStream())
                {
                    if (webStream != null)
                    {
                        using (StreamReader responseReader = new StreamReader(webStream))
                        {
                            string response = responseReader.ReadToEnd();
                            if (response.ToLower().Equals("true"))
                                return true;
                        }
                    }
                }
            }
            catch (Exception e)
            {
            }
            return false;
        }

        public bool XoaNhanVien(string maNV)
        {

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URLNhanVien + "/" + maNV);

            request.Method = "DELETE";
            request.ContentType = "application/json";

            try
            {
                WebResponse webResponse = request.GetResponse();
                using (Stream webStream = webResponse.GetResponseStream())
                {
                    if (webStream != null)
                    {
                        using (StreamReader responseReader = new StreamReader(webStream))
                        {
                            string response = responseReader.ReadToEnd();

                            if (response.ToLower().Equals("true"))
                                return true;
                        }
                    }
                }
            }
            catch (Exception e)
            {
            }
            return false;
        }

        public bool SuaNhanVien(NhanVienDTO nv)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URLNhanVien + "/" + nv.MANV);

            request.Method = "PUT";
            request.ContentType = "application/json";

            JObject jObject = JObject.FromObject(nv);
            string data = jObject.ToString(Newtonsoft.Json.Formatting.None);


            request.ContentLength = data.Length;




            using (Stream webStream = request.GetRequestStream())
            using (StreamWriter requestWriter = new StreamWriter(webStream, System.Text.Encoding.ASCII))
            {
                requestWriter.Write(data);
            }


            try
            {
                WebResponse webResponse = request.GetResponse();
                using (Stream webStream = webResponse.GetResponseStream())
                {
                    if (webStream != null)
                    {
                        using (StreamReader responseReader = new StreamReader(webStream))
                        {
                            string response = responseReader.ReadToEnd();
                            if (response.ToLower().Equals("true"))
                                return true;
                        }
                    }
                }
            }
            catch (Exception e)
            {
            }
            return false;

        }

        public int TinhLuong(int id, int thang, int nam)
        {
            int result = -1;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URLNhanVien + "/"
                + id.ToString()
                + "?thang=" + thang.ToString()
                + "&nam=" + nam.ToString());
            request.Method = "GET";
            request.ContentType = "application/json";

            try
            {
                WebResponse webResponse = request.GetResponse();
                using (Stream webStream = webResponse.GetResponseStream())
                {
                    if (webStream != null)
                    {
                        using (StreamReader responseReader = new StreamReader(webStream))
                        {

                            string response = responseReader.ReadToEnd();
                            result = JsonConvert.DeserializeObject<int>(response);

                        }
                    }
                }
            }
            catch (Exception e)
            {
            }

            return result;
            //throw new NotImplementedException();
        }
    }
}
